<?php

namespace app\api\controller;

use app\common\business\Quicklogin as quickloginBn;
use app\common\business\User as userBn;
use app\common\business\http\wechat\Wechat as wechatHttp;
class Quicklogin extends \app\common\controller\HomeBase {

    /**
     * 小程序登录
     */
    public function wxxcxlogin() {
        try {
            $post = input();
            //$post = '{"errMsg":"getUserInfo:ok","rawData":"{\"nickName\":\"别天神\",\"gender\":1,\"language\":\"zh_CN\",\"city\":\"Quanzhou\",\"province\":\"Fujian\",\"country\":\"China\",\"avatarUrl\":\"https://wx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEJT3DAxvEw3JIGRzDcuoiaMd5Kfwf0sd5uNoSCtgW2j30BJKGMn8K90Pg5AWLwovv2TiaF8On2ohSkA/132\"}","userInfo":{"nickName":"别天神","gender":1,"language":"zh_CN","city":"Quanzhou","province":"Fujian","country":"China","avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEJT3DAxvEw3JIGRzDcuoiaMd5Kfwf0sd5uNoSCtgW2j30BJKGMn8K90Pg5AWLwovv2TiaF8On2ohSkA/132"},"signature":"4b849b788c5143dccd0acf138ccdbb446a18b92a","encryptedData":"xPdfnqYuO9z6ZkNO5lMj6tLps+fOnsPyEpnjvDL4RqymjBG7NMlrBf2W8UI0I+o6MwBwtYwPEUSBPD9wNOXHvgrKUigkhG2PiBB3HvCodzBS5qkWoMUystKlIbUkTlhkB3x2GKOisEgIOpSvYDgmtCJr+Yz2TYs+ANiSPmkF4lKdKO5BZl5jHEa4G5973cZ8Th1bEaIyFT0d3S/cu5DDKQA10DR9SfAeQfhFItjLXkSDByG6ogH3bD1BCBDswn4ig2J5do3F+wKS0h90o0IWAoM15Do8ULUM12Vv/+Hyhbyug0/njxd8an44gnJ1zVDPo7MW6F3UH2MRzzplT9XSEcaoDebvpbFWQqWul662KfrTMfXndtKu5/16GsWhnOis6jT7ZwTirB2f9vjmJJLsqZz1hAOdKxDBeOX54MRWbcqlAqnDCYHlfsDBMzgvGI810mB6r2OR4kC1gp3yZc8o9+i5omR4VdW27g8ZzUl142OSVRwT9WikGTl3jVADtaKex6e9StmvM5dUizVqqSv0Iw==","iv":"NMtRB5noQ8t+Vjsu1yLvag==","code":"081DcajF0bbgIj2N2xkF0BhQiF0Dcajy"}';
            // $post = json_decode($post, true);
            if (!isset($post['code'])) {
                exception('登录失败', 110038);
            }

            $postdata = quickloginBn::checkWxXCX($post);
            //$post = $this->validate($post, 'QuickLogin.savequicklogin');
            $data = quickloginBn::savequicklogin($postdata);
            $this->_echoSuccessMessage('登陆成功', $data);
        } catch (\Exception $e) {
            $this->_echoErrorMessage($e->getMessage(), $e->getCode());
        }
    }
    
      /**
     * 获取微信code
     *
     */
    public function getcode() {
        $params = input();
        $wxopenid = new \app\extra\WeixinSDK\WxOpenidWap();
        $params['backurl'] = base64_decode($params['backurl']);
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        if (strpos($user_agent, 'MicroMessenger') === false) {
            $wxopenid->getcode($params['backurl']);
        } else {
            $wxopenid->getcodewx($params['backurl']);
        }
    }

    /**
     * 通过code获取微信用户 的信息
     * 
     */
    public function getwxuserinfo() {
        $params['code'] = isset($_REQUEST['code']) ? $_REQUEST['code'] : "";
        $params['openid'] = isset($_REQUEST['openid']) ? $_REQUEST['openid'] : "";
        $params['access_token'] = isset($_REQUEST['access_token']) ? $_REQUEST['access_token'] : "";
        $params['unionid'] = isset($_REQUEST['unionid']) ? $_REQUEST['unionid'] : "";
        $bn = new quickloginBn(); 
        $return = $bn->getwxinfo($params['code'], $params['access_token'], $params['openid'], $params['unionid']);

        $this->_echoSuccessMessage('', $return);
    }
    
     /**
     * 快捷登录
     * 测试地址：/wap/querylogin/login/type/qq
     */
    public function login() {
    
        $referer = isset($_REQUEST["http_referer"]) ? $_REQUEST["http_referer"] : '';  
             
        if (empty($referer)) {
            $referer = '/index/down/index';
        } else {
            $refererHttp = $referer;
            if (strpos($refererHttp, 'http') !== false) {
                $referer = $refererHttp;
            }  
        }
        setcookie('quickloginbackurl', $referer, time() + 7200, '/', '.qiu7.net'); 
        $querylogin = new wechatHttp();
        $backurl = 'https://xf2.qiu7.net/api/Quicklogin/loginback/type/weixin';
  
        // $backurl = 'http://m.qiu.vip/index/querylogin/loginback/type/weixin';
        $querylogin->getcode($backurl);
        exit;
    }

    /**
     * 快捷登录回调地址
     * 测试地址：/wap/querylogin/loginback/type/qq
     */
    public function loginback() {
        $params = input(); 
        //第三方回调 过来的code
        if (!isset($_REQUEST['code']) || empty($_REQUEST['code'])) {
            exception('参数错误', 100004);
        }
        $params['code'] = $_REQUEST['code'];
        $querylogin = new wechatHttp();
        $backurl = 'https://xf2.qiu7.net/api/Quicklogin/loginback/';
        $data = $querylogin->getwxuserinfo($params['code']); 
        if (isset($data['sso']) && !empty($data['sso'])) {
            $this->_setSso($data['sso']);
            //登录成功跳回首页
            $referer = empty($_COOKIE['quickloginbackurl']) ? '/' : $_COOKIE['quickloginbackurl'];
            header('Location: ' . $referer."#/get_data/0/".base64_encode($data['sso']));
            exit;
        }
        if (!isset($data['unionid'])) {
            //登录成功跳回首页
            header('Location: /member/login');
            exit;
        }
        header('Location: /');
        exit;
        $data['type'] = $params['type'];
        header('Location: /querylogin/bind/unionid/' . $data['unionid'] . '/type/' . $params['type'] . "/wxuserinfo/" . base64_encode(json_encode($data)) . '/?http_referer=' . $referer);
        exit;
    }


}
